package cn.tedu.tea.service;

import cn.tedu.tea.domain.Comment;
import cn.tedu.tea.domain.param.CommentAddNewDTO;
import cn.tedu.tea.domain.param.CommentUpdateDTO;
import cn.tedu.tea.domain.vo.CommentListVO;
import cn.tedu.tea.domain.vo.CommentStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.CommentMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void addNew(CommentAddNewDTO commentAddNewDTO) {
        log.debug("开始处理【添加评论】的业务，参数： {}", commentAddNewDTO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewDTO, comment);
        comment.setModifiedUserId(0L).setModifiedUserName("");
        int rows = commentMapper.insert(comment);
        if (rows != 1){
            String message = "添加评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除评论】的业务，参数： {}", id);
        CommentStandardVO commentStandard = commentMapper.getStandardById(id);
        if (commentStandard == null){
            String message = "删除评论失败，要删除的评论不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = commentMapper.deleteById(id);
        if (rows != 1){
            String message = "删除评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除评论】的业务，参数： {}", ids);
        int rows = commentMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(CommentUpdateDTO commentUpdateDTO) {
        log.debug("开始处理【修改评论信息】的业务，参数：{}", commentUpdateDTO);
        CommentStandardVO commentStandard = commentMapper.getStandardById(commentUpdateDTO.getId());
        if (commentStandard == null){
            String message = "修改评论失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentUpdateDTO, comment);
        int rows = commentMapper.update(comment);
        if (rows != 1){
            String message = "修改评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将评论设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将评论设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public CommentStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询评论详情】的业务，参数： {}", id);
        CommentStandardVO commentStandard = commentMapper.getStandardById(id);
        if (commentStandard == null){
            String message = "查询评论详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return commentStandard;
    }

    @Override
    public List<CommentListVO> list() {
        log.debug("开始处理【查询评论列表】的业务，无参数");
        List<CommentListVO> list = commentMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        CommentStandardVO currentComment = commentMapper.getStandardById(id);
        if (currentComment == null) {
            String message = STATUS_TEXT[status] + "评论失败，评论数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentComment.getStatus() == status) {
            String message = STATUS_TEXT[status] + "评论失败，此评论已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Comment updateComment = new Comment();
        updateComment.setId(id);
        updateComment.setStatus(status);
        int rows = commentMapper.update(updateComment);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "评论失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
